﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;

namespace POSPlus.EZBarTender.DatabaseOperations
{
	public class Product : DatabaseObject
	{
		public String Upc { get; set; }
		public Double NormalPrice { get; set; }
		public Double GroupPrice { get; set; }
		public Int16 PriceMethod { get; set; }
		public Int16 Quantity { get; set; }
		public String Size { get; set; }
		public String Uom { get; set; }
		public String Description { get; set; }

		public Product(String connectionString)
		{
			ConnectionString = connectionString;
			PopulateDefault();
		}

		public Product(String connectionString, String upc)
		{
			ConnectionString = connectionString;
			using (SqlConnection connection = new SqlConnection(ConnectionString))
			{
				connection.Open();
				using (SqlCommand command = new SqlCommand("SELECT Upc, Description, Normal_Price, PriceMethod, GroupPrice, Quantity, " +
					"Size, Uom FROM Products WHERE Upc = @Upc", connection))
				{
					command.Parameters.AddWithValue("@Upc", upc);
					using (SqlDataReader reader = command.ExecuteReader())
					{
						if (reader.Read())
						{
							Upc = DatabaseObject.FromDB<String>(reader["Upc"]);
							NormalPrice = (double)DatabaseObject.FromDB<decimal>(reader["Normal_Price"]);
							PriceMethod = DatabaseObject.FromDB<Int16>(reader["PriceMethod"]);
							GroupPrice = (double)DatabaseObject.FromDB<decimal>(reader["GroupPrice"]);
							Quantity = DatabaseObject.FromDB<Int16>(reader["Quantity"]);
							Size = DatabaseObject.FromDB<String>(reader["Size"]);
							Uom = DatabaseObject.FromDB<String>(reader["Uom"]);
							Description = FromDB<String>(reader["Description"]);
						}
						else
							PopulateDefault();
						reader.Close();
					}
				}
				connection.Close();
			}
		}
		protected override void PopulateDefault()
		{
			Upc = "";
			NormalPrice = -1;
			GroupPrice = -1;
			PriceMethod = -1;
			Quantity = -1;
			Size = "";
			Uom = "";
			Description = "";
		}

		public override Boolean Create()
		{
			throw new NotImplementedException();
		}

		public override Boolean Update()
		{
			throw new NotImplementedException();
		}

		public override Boolean Delete()
		{
			throw new NotImplementedException();
		}
	}
}
